﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace QLKS
{
   
    public partial class ThanhToan : Form
    {
        public Mysql qmysql = new Mysql();
        public int MP = 0;
        public double tien = 0;
        public ThanhToan()
        {
            InitializeComponent();
        }

        private void ThanhToan_Load(object sender, EventArgs e)
        {
            //MaP 	MaLP 	MaPNP 	TenLP 	TenP 	TenKH 	CMND 	Giagio 	Giangay 	Ngayvao 	Ngayra 
            int RoomID = DMP.BTName;
            var vietnam = System.Globalization.CultureInfo.GetCultureInfo("vi-VN");
            DataTable Data = qmysql.GetDataRoomID(RoomID);
            foreach (DataRow row in Data.Rows)
            {
                MP = Convert.ToInt32(row["MaP"]);
                lb_SoHoaDon.Text = row["MaPNP"].ToString();
                lb_RoomNumber.Text = row["TenP"].ToString();
                lb_CName.Text = row["TenKH"].ToString();
                lb_CM.Text = row["CMND"].ToString();
                // Ngay ra sau cung;
                DateTime nr = DateTime.Now;
                lb_NgayRa.Text = nr.ToString("HH:mm:ss dd-MM-yyyy");
                // Ngay vao;
                DateTime dt = Convert.ToDateTime(row["Ngayvao"].ToString());
                lb_NgayVao.Text = dt.ToString("HH:mm:ss dd-MM-yyyy");
                // So gio o lai;
                int sogio = Convert.ToInt32(nr.Subtract(dt).TotalHours);
                if (sogio < 1)
                    sogio = 1;

                if (sogio <= 24)
                {
                    int dongia = Convert.ToInt32(row["Giagio"]);
                    lb_DonGia.Text = dongia.ToString("C0", vietnam) + "/Giờ.";
                    tien = sogio * Convert.ToDouble(row["Giagio"]);
                    lb_tgo.Text = sogio + " Giờ";
                }
                else
                {
                    int dongia = Convert.ToInt32(row["Giangay"]);
                    lb_DonGia.Text = dongia.ToString("C0", vietnam) + "/Ngày.";
                    if (sogio % 24 == 0)
                    {
                        tien = sogio / 24 * Convert.ToDouble(row["Giangay"]);
                        lb_tgo.Text = sogio/24 + " Ngày";
                    }
                    else
                    {
                        tien = ((sogio / 24) + 1) * Convert.ToDouble(row["Giangay"]);
                        lb_tgo.Text = ((sogio / 24) + 1) + " Ngày";
                    }
                }
                lb_tt.Text = tien.ToString("C0", vietnam);
                if (!File.Exists(@"Info.ini"))
                {
                    // Chua co thong tin khach san;
                    FileStream Infofile = File.Create(@"Info.ini");
                    StreamWriter wr = new StreamWriter(Infofile);
                    wr.WriteLine("Sheraton Saigon Hotel And Towers");
                    wr.WriteLine("88 Đồng khởi, Quận 1, TP Hồ Chí Minh.");
                    wr.WriteLine("0999 999 999");
                    wr.Close();
                    Infofile.Close();
                }
                else
                {
                    // Đã có file;
                    FileStream furl = File.OpenRead(@"Info.ini");
                    StreamReader sr = new StreamReader(furl);
                    lb_ksname.Text = sr.ReadLine();
                    lb_adrr.Text = sr.ReadLine();
                    lb_phone.Text = sr.ReadLine();
                    sr.Close();
                    furl.Close();
                }
            }
            //
                
           
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
           
            this.Close();
        }
        

        private void btn_CheckOut_Click(object sender, EventArgs e)
        {
            DialogResult QuitNP = MessageBox.Show("Bạn có chắc sẽ thanh toán phòng này không ?!","Thông báo",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
            if (QuitNP == DialogResult.OK)
            {
               // Cập nhật CSQL;
                // Xóa phiếu nhận phòng.
                string money = tien.ToString();
                // Ngay ra sau cung;
                DateTime nr = DateTime.Now;
                string cmdTextIsnv = "UPDATE ht_phieunp SET Trangthai='0', Thanhtien=?money, Ngayra=?ngayra WHERE MaPNP =mapnp";
                Dictionary<string, string> Params_dell = new Dictionary<string, string>();
                Params_dell["money"] = money;
                Params_dell["mapnp"] = MP.ToString();
                Params_dell["ngayra"] = nr.ToString();
                this.qmysql.NonQueryMysqlCommand(cmdTextIsnv, Params_dell);
                // Thieu phan reload;
                DMP dmp = new DMP();
                dmp.MdiParent = this.ParentForm;
                dmp.Show();
                this.Close();

            }
                
            
                
        }
       
        
    }
}
